﻿using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Joker.View {
    public partial class ReviewForm : Form {
        public string currentTable;
        public string sqlstr;
        public ReviewForm() {
            InitializeComponent();
        }
        public void RebuildSqlStr() {
            if (comboBox1.SelectedItem.Equals("采购审核")) {
                currentTable = "[Purchase]";
            } else if (comboBox1.SelectedItem.Equals("入库审核")) {
                currentTable = "[WarehouseIn]";
            } else if (comboBox1.SelectedItem.Equals("出库审核")) {
                currentTable = "[WarehouseOut]";
            } else {
                throw new Exception($"意料之外的值:\n{comboBox1.SelectedItem}");
            }
            sqlstr = $"select [ID],[Date],[State] from {currentTable} where [Date] between cast('{dateTimePicker1.Value.ToString("yyyy-MM-dd")}' as date) and cast('{dateTimePicker2.Value.ToString("yyyy-MM-dd")}' as date) and [State] = '{comboBox2.SelectedItem.Equals("已审核")}'";
        }

        private void ReviewForm_Load(object sender, EventArgs e) {
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            dateTimePicker1.Value = DateTime.Now.AddDays(-365);
            RebuildSqlStr();
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlstr);
        }

        private void button1_Click(object sender, EventArgs e) {
            RebuildSqlStr();
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlstr);
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
            DataGridView dgv = (DataGridView)sender;
            if (dgv.CurrentRow == null || e.RowIndex < 0 || e.ColumnIndex < 0 || e.RowIndex > dgv.Rows.Count || e.ColumnIndex >= dgv.Rows[0].Cells.Count) return;
            if (e.Button.Equals(MouseButtons.Right)) {
                dgv.CurrentCell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if (dgv.CurrentRow.Cells[2].Value.ToString().Equals("True")) {
                    contextMenuStrip1.Items[0].Text = "撤销批准";
                } else {
                    contextMenuStrip1.Items[0].Text = "批准";
                }
                contextMenuStrip1.Show(Cursor.Position);
            } else {
                dataGridView1.Focus();
                return;
            }
        }

        private void RightMenu_Click(object sender, EventArgs e) {
            if (contextMenuStrip1.Items[0].Text.Equals("批准")) {
                bool result = true;
                result &= DBHelper.ExecuteNonQuery($"update {currentTable} set [State] = 'true' where [ID] = {dataGridView1.CurrentRow.Cells[0].Value}");
                if (comboBox1.SelectedItem.Equals("入库审核")) {
                    result &= DBHelper.ExecuteNonQuery($"INSERT [Inventory] VALUES(CAST('{DateTime.Today.ToString("yyyy-MM-dd")}' AS DATE),{dataGridView1.CurrentRow.Cells[0].Value})");
                }
                if (result) {
                    MessageBox.Show("审批成功!");
                } else {
                    MessageBox.Show("审批失败!");
                }
            } else {
                if (DBHelper.ExecuteNonQuery($"update {currentTable} set [State] = 'false' where [ID] = {dataGridView1.CurrentRow.Cells[0].Value}")) {
                    MessageBox.Show("撤销成功!");
                } else {
                    MessageBox.Show("撤销失败!");
                }
            }
            RebuildSqlStr();
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlstr);
        }
    }
}
